var statuedic;
$(function(){
	statuedic = getDictionaryByType('EVAL_TASTAUDIT_STATUE');
	$(".nav-ul li").eq(4).addClass("cur");
    if ($(".form-inline div").hasClass('form-more')) {
        $('.btn-more').show()
    }
    $('.btn-clear-form').click(function () {
        $('.form-inline input').val('')
    })
	
	query(1);
    
    $('#pageNum').dropdown();
	$('#dropdown').on('shown.bs.dropdown', function () {
	     $('.dropdown-menu li').click(function () {
	         var str = $(this).children('a').html();
	         var size = $(this).children('a').children('label').text();
	         str += '<span class="caret"></span>';
	         $("#pageNum").html(str);
	         
	         query(1);
	     })
	 });
	
	$("#workFlow").on("show.bs.modal",function(e){
//		 这里的btn就是触发元素，即你点击工单流转按钮
	    var btn = $(e.relatedTarget);
	    var singleTaskId = btn.attr("value-id");
	    if(singleTaskId){
	    	$.ajax({
	    		type : "POST",
	    		url : "evalTaskFlow/getBySingleTaskId",
	    		data : {
	    			"singleTaskId" : singleTaskId
	    		},
	    		traditional: true,
	    		dataType : "json",
	    		success : function(data) {
	    			if (data.flag) {
	    				var tread = data.datas;
						insertInfoTpl(tread, $('#workFlowTread'),$('#workFlowTreadTpl'));
	    			}
	    		},
	    		error : function(data) {
	    			layer.closeAll();
	    		}
	    	});
	    }
	});
	
	laydate.render({
        elem: '#firstTime1', 
        type: 'datetime'
      });
      var firstTime1 = $('#firstTime1').text();
      laydate.render({
        elem: '#firstTime2', 
        min: firstTime1,
        type: 'datetime'
      });
      if($.cookie('return_statue')){
   	   	$("#statue").val($.cookie('return_statue'));
      }
})

function query(page){
	if($("#statue").val() == ''){
		$("#statue").val($.cookie('return_statue'));
	}
	$.cookie('statue', $("#statue").val());
	layer.load(4);
	$.ajax({
		type : "POST",
		url :  "evalTaskAudit/getTaskAuditInfo",
		data : {
			'currentPage': page,
			'pageSize' : $("#pageNum").children('strong').text(),
			'businessType': $("#businessType").val(),
			//'statue': $("#statue").val(),
			'statue': $.cookie('statue'),
			'controctor': $("#controctor").val(),
			'address': $("#address").val(),
			'companyName' : $("#companyName").val(),
			'createTimeBefore': $('#firstTime1').val(),
			'createTimeAfter': $('#firstTime2').val(),
			"formNum" : $("#formNum").val(),
			"business" : $("#business").val()
		},
		dataType : "json",
		success : function(data) {
			layer.closeAll();
			if (data.flag) {
				var tread = data.datas;
				insertInfoTpl(tread, $('#tread'), $('#treadTpl'));
				var currentPage = data.currentPage;
				var totalPages = data.totalPages;
				var totalData = data.totalData;
				$("#total_data").text(totalData);
				$("#total_pages").text(totalPages);
				var options = {
					currentPage : currentPage, // 当前的请求页面。
					totalPages : totalPages, // 一共多少页。
					size : "normal",  // 应该是页眉的大小。
					bootstrapMajorVersion : 3, // bootstrap的版本要求。
					alignment : "right",
					numberOfPages : 5, // 一页显示几个按钮
					itemTexts : function(type, page, current) { // 如下的代码是将页眉显示的中文显示我们自定义的中文。
						switch (type) {
						case "first":
							return "首页";
						case "prev":
							return "上一页";
						case "next":
							return "下一页";
						case "last":
							return "末页";
						case "page":
							return page;
						}
					},
					tooltipTitles : function(type, page, current) { // 提示框，当鼠标放上去给提示
						switch (type) {
						case "first":
							return "首页";
						case "prev":
							return "上一页";
						case "next":
							return "下一页";
						case "last":
							return "末页";
						}
					},
					onPageClicked : function(event, originalEvent, type, page) {
						layer.load(4);
						$.ajax({
							type : "POST",
							url : "evalTaskAudit/getTaskAuditInfo",
							data : {
								'currentPage': page,
								'pageSize' : $("#pageNum").children('strong').text(),
								'businessType': $("#businessType").val(),
								//'statue': $("#statue").val(),
								'statue': $.cookie('statue'),
								'controctor': $("#controctor").val(),
								'address': $("#address").val(),
								'companyName' : $("#companyName").val(),
								'createTimeBefore': $('#firstTime1').val(),
								'createTimeAfter': $('#firstTime2').val(),
								"formNum" : $("#formNum").val(),
								"business" : $("#business").val()
							},
							dataType : "json",
							success : function(data) {
								if (data.flag) {
									layer.closeAll();
									var tread = data.datas;
									insertInfoTpl(tread, $('#tread'),$('#treadTpl'));
								}
							},
							error : function(data) {
								layer.closeAll();
							}
						});
					}
				}
				$('#pageLimit').bootstrapPaginator(options);
			}
		},
		error : function(data) {
			layer.closeAll();
		}
	});
}

function jumpQuery(){
	var page = $("#jump_page").val();
	var reg = /^\+?[1-9][0-9]*$/;//判断是否为正整数 
    var flag = reg.test(trim(page));
    if(!flag){
    	layer.alert("请输入正整数！", {
			icon : 3,
			offset: '100px'
		});
    }else{
    	var totalPages = $("#total_pages").text();
    	if(parseInt(page) > parseInt(totalPages)){
    		layer.alert("跳转页数不能超过总页数！", {
    			icon : 3,
    			offset: '100px'
    		});
    	}else{
    		query(page);
    	}
    }
}

function exportData(){
	layer.load(4);
	$.ajax({
		type : "POST",
		url : "evalTaskAudit/exportData",
		dataType : "json",
		success : function(data) {
			layer.closeAll();
			if (data.flag) {
				var url = "evalTaskAudit/downFiles?path="+ data.path;
				window.location.href = url;
			} else {
				alert(data.msgs);
			}
		},
		error : function(data) {
			layer.closeAll();
		}
	});
}

/**
 * [insertInfoTpl 插入 html信息]
 * @param  {[type]} data [渲染所需的数据]
 * @param  {[type]} dom  [加入页面中的哪个DOM节点]
 * @return {[type]}      [description]
 */
function insertInfoTpl(data, dom, tpl) {
	dom.empty();
    var infoTpl = tpl.html()
    var compiled = _.template(infoTpl)
    var html = ''
    var columnArray = new Array();	
    _.forEach(data, function (item, index) {
        item.index = index + 1
        item.statueName = getValueByKey(statuedic,item.statue+'');
        // console.log(item, index)
        var map = {};
        map['statue'] = item.statue;
        map['id'] = item.id;
        columnArray.push(map)
        html += compiled(item)
    })
    dom.append($(html))
    for(var i=0; i<columnArray.length; i++){
    	setBtnShow(columnArray[i].statue, columnArray[i].id);
    }
}

/**
 * 查看工单
 * showType：view表示当前查看
 */
function showApplication(singleTaskId, type, statue){
	var param = "?singleTaskId="+singleTaskId+"&statue="+statue+"&type="+type+"&backViewType=2&showType=view";
	/*if(statue == 4 || statue == 8){ 
		window.location.replace("evalProject"+param);
	} else { 
		window.location.replace("evalApplication"+param);
	}*/
	window.location.replace("evalApplication"+param);
	
}

function showEval(singleTaskId, type, statue){
	var param = "?singleTaskId="+singleTaskId+"&statue="+statue+"&type="+type+"&backViewType=2";
	//修改新增5-测试通过 6-测试不通过，看的是评测记录
	if(statue == 4 || statue == 8 || statue == 5 || statue == 6){ 
		window.location.replace("evalProject"+param);
	} else { 
		window.location.replace("evalApplication"+param);
	}
	//window.location.replace("evalApplication"+param);
	
}

function showResult(singleTaskId, type, statue){
	var param = "?singleTaskId="+singleTaskId+"&statue="+statue+"&type="+type+"&backViewType=2";
	window.location.replace("evalApplication"+param);
}

/**
 *1-未审核 2-已核实(未评测) 3-退回修改 4-评测中 5-测试通过 6-测试不通过 7-退回补正 8-已补正 
 */
function setBtnShow(statue, id){
//	if(statue == 1 || statue == 2 || statue == 4 || statue == 9){ // 填写
//		$('#write_'+id).removeAttr('style');
//	} 
//	if(statue == 1 || statue == 2){ // 删除
//		$('#del_'+id).removeAttr('style');
//	}
//	if(statue == 2 || statue == 4 || statue == 9){ // 提交
//		$('#submit_'+id).removeAttr('style');
//	}
//	if(statue == 3 || statue == 5 || statue == 6 || statue == 7 || statue == 8 || statue == 10){ //查看
//		$('#view_'+id).removeAttr('style');
//	}
//	if(statue == 3 || statue ==4 || statue == 5 || statue == 6 || statue == 7 || statue == 8 || statue == 10){ //工单流转
//		$('#flow_'+id).attr('style','width: 25%;');
//	}

	if(statue == 5 || statue == 6){ // 重新评测
		$('#againEval_'+id).attr('style','width: 20%;');
	}
    if(statue == 4 || statue == 5 || statue == 6 || statue == 8){ //评测记录
		$('#evalRecord_'+id).attr('style','width: 20%;');
	}
    if(statue == 5 || statue == 6){//评测报告
    	$('#evalResult_'+id).attr('style','width: 20%;');
    }
}



function trim(str){ 
    return str.replace(/(^\s*)|(\s*$)/g, ""); 
}

/**
 * 提交
 * @param id
 * @param statue 当前状态
 */
function addTaskAudit(id, statue){
	//修改为要改变的状态
	var btnName = '提交';
    if(statue == 5 || statue == 6){
		btnName = '重新提测';
		statue = 6;
		if(!confirm("确定重新提测吗？")){
			return false;
		}
	}
	$.ajax({
		type : "POST",
		url : "evalApplication/submitCdn",
		data : {
			"singleTaskId" : id,
			"statue": statue,
			"btnName": btnName
		},
		traditional: true,
		dataType : "json",
		success : function(data) {
			if (data.flag) {
				query(1);
			}
		},
		error : function(data) {
			layer.closeAll();
		}
	});
}
